package ro.ubbcluj.cs.damate.plugin.utils;

import org.eclipse.core.runtime.IStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import ro.ubbcluj.cs.damate.plugin.DamateActivator;

/**
 * @author Lorand Fazakas
 */
@Service
@Scope("singleton")
public class DamateLogger {

    @Autowired
    @Qualifier("pluginLogLevel")
    private String logLevel;

    public enum Severity {
        OK(IStatus.OK),
        INFO(IStatus.INFO),
        WARNING(IStatus.WARNING),
        ERROR(IStatus.ERROR),
        CANCEL(IStatus.CANCEL);

        int eclipseSeverityCode;

        Severity(int eclipseSeverityCode) {
            this.eclipseSeverityCode = eclipseSeverityCode;
        }
    }

    private Severity getLogLevelAsSeverity() {
        Severity logLevelSeverity = Severity.WARNING;
        try {
            logLevelSeverity = Severity.valueOf(logLevel.toUpperCase());
        } catch (Exception e) {}
        return logLevelSeverity;
    }

    public void info(String message) {
        log(Severity.INFO, message);
    }

    public void info(String message, Throwable e) {
        log(Severity.INFO, message, e);
    }

    public void warning(String message) {
        log(Severity.WARNING, message);
    }

    public void warning(String message, Throwable e) {
        log(Severity.WARNING, message, e);
    }

    public void error(String message) {
        log(Severity.ERROR, message);
    }

    public void error(String message, Throwable e) {
        log(Severity.ERROR, message, e);
    }

    public void log(Severity severity, String message) {
        log(severity, message, null);
    }

    public void log(Severity severity, String message, Throwable e) {
        if (getLogLevelAsSeverity().eclipseSeverityCode <= severity.eclipseSeverityCode) {
            DamateActivator.log(message, severity.eclipseSeverityCode, e);
        }
    }
}
